<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Site extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->title = "SMILE | Selamat datang, telkom siap melayani Anda!";
    }

    /* PAGE-RELATED FUNCTION */

    public function index() {
        //if ($this->session->userdata('is_login_plasa')!=null) {
        if ($this->session->userdata('is_plasa')) {
            if (count($this->session->userdata('answers'))) {
                redirect("site/questionnaire");
            } else {
                $this->welcome_page();
            }
        } else {
            $this->admin();
        }
    }

    public function admin() {
        $data['is_plasa'] = $this->session->userdata('is_plasa');
        $this->load->view("login_plasa", $data);
    }

    public function logout_plasa() {
        $this->session->sess_destroy();
        redirect("site");
    }

    public function welcome_page() {
        if (!$this->session->userdata('is_plasa')) {
            redirect("site/admin");
        } else {
            $this->load->view('start');
        }
    }

    public function customer_service() {
        $param['csrs'] = $this->csrmodel->get_all_csr_by_plasa_nojoin($this->session->userdata("is_plasa"))->result_array();

        $this->load->view('ask_csr', $param);
    }

    // call this by insert "site/questionnaire_end" in view
    public function questionnaire_end() { // executed once when user close
        if (!$this->session->userdata('is_plasa')) {
            redirect("site/admin");
        } else {
            $this->session->unset_userdata('answers');
            $this->session->unset_userdata('sum_answered');
            $this->session->unset_userdata('answer_csr');
            $this->session->unset_userdata('user_role');
            $this->session->unset_userdata('sum_question');

            // set homepage here 
            $this->load->view('finish');
        }
    }

    public function set_csr() {
        $this->layout = false;
        $this->session->set_userdata('user_role', 'customer');
        $this->session->set_userdata('answer_csr', $this->input->post('csr_id'));
        echo "succ";
    }

    public function questionnaire() {// executed once for customer
        if (!$this->session->userdata('is_plasa')) {
            redirect("site/admin");
        } else if (!$this->session->userdata("answer_csr")) {
            redirect("site/welcome_page");
        } else {
            //echo $this->session->userdata("answer_csr");
            $questions = $this->questionmodel->get_all_visible_question();
            $sum_question = $this->questionmodel->get_sum_all_visible_question();


            $this->session->set_userdata('sum_question', $sum_question);
            //$this->session->set_userdata('sum_answered', '0');
            /* $this->session->set_userdata('questions', $questions); */
            $this->session->set_userdata('answers', array());

            if (count($questions) > 0) {
                //preparing parameters
                $row_question = $questions[0];
                $param['question_id'] = $row_question['question_id'];
                $param['question_content'] = $row_question['content'];
                $param['is_empty'] = "0";
            } else {
                $param['is_empty'] = "1";
            }

            //set page here
            $this->load->view('responds', $param);
        }
    }

    /* END LOAD-PAGE-RELATED FUNCTION */

    /* AJAX-RELATED FUNCTIONS */

    public function ajax_plasa_login() {
        $this->layout = false;
        $userplasa = $this->input->post("userplasa");
        $password = $this->input->post("password");

        if ($this->usermodel->is_login_correct($userplasa, $password)) {
            $this->session->set_userdata("is_plasa", $this->usermodel->get_plasa_id_by_username($userplasa));
            echo 'succ';
        } else {
            echo 'err';
        }
    }

    public function ajax_questionnaire_next() {
        $this->layout = false;

        $question_id = $this->input->post('question_id');
        $question_answer = $this->input->post('question_answer');
        $sum_answered = $this->input->post('sum_answered');


        $sum_question = $this->session->userdata('sum_question');
        $answers = $this->session->userdata('answers');
        //echo $question_id." dan ".$question_answer;
        $added_answer = array($question_id, $question_answer);
        array_push($answers, $added_answer);
        $this->session->set_userdata('answers', $answers);

        ++$sum_answered;

        // check whether last question or not
        if ($sum_answered == $sum_question) { //is last question
            // call single function to automatically add all answers
            if ($this->session->userdata('answer_csr') !== null) {
                $this->respondmodel->add_all_respond($this->session->userdata('answer_csr'), $answers);
            } else {
                redirect("site/customer_service");
            }
            // send feedback to js
            echo 'succ_end';
        } else { //is not last question
            $questions = $this->questionmodel->get_all_visible_question();
            $row_question = $questions[$sum_answered];
            $question_id = $row_question['question_id'];

            echo $question_id;
        }
    }

    public function ajax_is_locked_question() {
        $question_id = $this->input->post('question_id');
        return $this->questionmodel->is_respond_locked($question_id);
    }

    public function ajax_get_question() {
        $this->layout = false;

        // sending question content to javascript
        $question_id = $this->input->post('question_id');
        $questions = $this->questionmodel->get_info_question($question_id);
        $row_question = $questions->row(0);

        echo $row_question->content;
    }

    public function ajax_get_csr_by_plasa() {
        $plasa_id = $this->session->userdata("is_admin") ? $this->session->userdata("is_admin") : $this->session->userdata("is_pic");

        $result = $this->csrmodel->get_all_csr_by_plasa_nojoin($plasa_id);

        echo json_encode($result->result_array());
    }

    /* END OF AJAX-RELATED FUNCTIONS */
}

/* End of file site.php */
/* Location: ./application/controllers/site.php */